package cn.jji8.QSGUI.quick.command;

import cn.jji8.QSGUI.quick.logger.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;

/**
 * 使用命令管理器可以更方便的管理命令，节约写命令补全器和命令执行器的时间。
 * */
public class CommandManger implements CommandExecutor, TabCompleter {
    public static void load(JavaPlugin main,String mainCommand){
        Logger.putInfo("命令控制器开始加载..");
        CommandManger commandManger = new CommandManger();
        Bukkit.getPluginCommand(mainCommand).setExecutor(commandManger);
        Bukkit.getPluginCommand(mainCommand).setTabCompleter(commandManger);
        Logger.putInfo("命令控制器加载完成。");
    }
    /**
     * 一个命令列表 和调用器
     * */
    static Map<String[],commandAndCompletion> commandMap = new HashMap<>();
    /**
     * 添加一个命令
     * @param commandList 用于触发的命令列表
     * @param commandImplement 命令执行器
     * @param completion 补全列表{{位置1补全参数},{位置2补全参数},{位置3补全参数}}
     */
    public static void addCommand(String[] commandList,CommandImplement commandImplement,String[][] completion){
        commandMap.put(commandList,new commandAndCompletion(completion,commandImplement));
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if(strings.length>=1){
            Set<String[]> commandSet = commandMap.keySet();
            for(String[] list:commandSet){
                if(equalsOneIgnoreCase(strings[0],list)){
                    String[] strings1 = new String[strings.length-1];
                    for(int i=0;i<strings1.length;i++){
                        strings1[i] = strings[i+1];
                    }
                    commandMap.get(list).commandImplement.command(commandSender,strings1);
                }
            }

        }
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
        List<String> commandList = new ArrayList<>();
        Set<String[]> commandSet = commandMap.keySet();
        if(strings.length==1){
            for(String[] list:commandSet){
                for(String string:list){
                    commandList.add(string);
                }
            }
        }else if(strings.length>1){
            for(String[] list:commandSet){
                if(equalsOneIgnoreCase(strings[0],list)){
                    String[][] completion = commandMap.get(list).completion;
                    int position = strings.length-2;
                    if(position<completion.length){
                        String[] stringss = completion[position];
                        for(String a:stringss){
                            commandList.add(a);
                        }
                    }
                }
            }

        }
        return commandList;
    }

    /**
     * 比较器，只要字符与字符列表的其中一个相同就返回true
     * */
    static boolean equalsOneIgnoreCase(String s,String[] strings){
        for(String s1:strings){
            if(s.equalsIgnoreCase(s1)){
                return true;
            }
        }
        return false;
    }
}
